package com.hol.web;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;

import org.apache.commons.lang.StringUtils;

import com.hol.bean.Record;
import com.hol.dao.support.Page;
import com.hol.entity.Article;
import com.hol.entity.Dictionary;
import com.hol.json.Jackson;
import com.hol.mvc.Annotation.JSONOutput;
import com.hol.mvc.RequestContext;
import com.hol.service.ArticleService;
import com.hol.util.ResponseUtils;

public class ArticleAction {
	
	private ArticleService articleService = new ArticleService();

	/**
	 * 增加或修改
	 * @throws ParseException 
	 * */
	public void save(RequestContext rc) throws ServletException, IOException, ParseException {
		// 将请求参数自动包装成Bean对象
		Article article = rc.form(Article.class);
		if(article.getArtId()==null || article.getArtId()==0) {
			// 增加
			articleService.add(article);
		} else {
			// 修改
			articleService.update(article);
		}
		
		// 输出JSON
		String json = Jackson.me().toJson(new Record(true));
		ResponseUtils.renderJson(rc.response(), json);
	}
	
	public void save2(RequestContext rc) throws ServletException, IOException, ParseException {
		// 将请求参数自动包装成Bean对象
		Article article = rc.form(Article.class);
		DateFormat bf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		String format=bf.format(date);
		Date comDate=bf.parse(format);
		article.setArtDate(comDate);
		if(article.getArtId()==null || article.getArtId()==0) {
			// 增加
			articleService.add(article);
		} else {
			// 修改
			articleService.update(article);
		}
		
		// 输出JSON
		String json = Jackson.me().toJson(new Record(true));
		ResponseUtils.renderJson(rc.response(), json);
	}
	/**
	 * 后台显示
	 * */
	public void list(RequestContext rc) throws ServletException, IOException {
		Long id=rc.param("id",0L);
		int pageNo = rc.param("page", 1);
		int pageSize = rc.param("limit", 15);
		String orderBy = rc.param("orderBy", "");
		String orderDir = rc.param("orderDir", "");
		
		Page<Article> page = new Page<>(pageNo, pageSize);
		if(StringUtils.isNotBlank(orderBy)) {
			page.setOrderBy(orderBy);
			page.setOrder(orderDir);
		}
		
		Record record = articleService.findPage(page,id);
		// 输出JSON
		String json = Jackson.me()
				.with(Dictionary.class, "itemValue").toJson(record);
		ResponseUtils.renderJson(rc.response(), json);
		
	}
	//前台显示
	public void list2(RequestContext rc) throws ServletException, IOException {
		Long id=rc.param("id",0L);
		System.out.println(id);
		int pageNo = rc.param("page", 1);
		int pageSize = rc.param("limit", 15);
		String orderBy = rc.param("orderBy", "");
		String orderDir = rc.param("orderDir", "");
		
		Page<Article> page = new Page<>(pageNo, pageSize);
		if(StringUtils.isNotBlank(orderBy)) {
			page.setOrderBy(orderBy);
			page.setOrder(orderDir);
		}
		
		Record record = articleService.findPage2(page,id);
		// 输出JSON
		String json = Jackson.me()
				.with(Dictionary.class, "itemValue").toJson(record);
		ResponseUtils.renderJson(rc.response(), json);
		
	}
	
	
	/**
	 * 删除
	 * */
	public void del(RequestContext rc) throws ServletException, IOException {
		long id = rc.param("id", 0L);
		articleService.remove(id);
		
		// 返回JSON
		String json = Jackson.me().toJson(new Record(true));
		ResponseUtils.renderJson(rc.response(), json);
	}
	
	/**
	 * 修改之前，获得信息，用于回显
	 * */
	public void get(RequestContext rc) throws ServletException, IOException {
		
		String id = rc.param("id");
		if(id!=null && !id.equals("")) {
			Article article = articleService.findById(Long.parseLong(id));
			rc.reqAttr("article", article);
			rc.forward("/background/article_form.jsp");
		}
	}
public void get2(RequestContext rc) throws ServletException, IOException {
		
		String id = rc.param("id");
		if(id!=null && !id.equals("")) {
			Article article = articleService.findById(Long.parseLong(id));
			rc.reqAttr("article", article);
			rc.forward("/foreground/article_form.jsp");
		}
	}
	
	/**
	 * 批量删除
	 * */
	@JSONOutput
	public Record multiDel(RequestContext rc) throws ServletException, IOException {
		String ids = rc.param("ids");
		if(StringUtils.isNotBlank(ids)) {
			articleService.multiDel(ids);
		}		
		return new Record(true);
	}
	/**
	 * 查看文章
	 * */
	public void view(RequestContext rc) throws ServletException, IOException {
		Long id = rc.param("id", 0L);
		Article article = articleService.findById(id);
		rc.reqAttr("article", article);
		rc.forward("/foreground/blog_detail.jsp");
	}

}
